/*
 * 文件名：BpastCostCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.bpast.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.DeviceSureService;
import top.infopub.job.bpa.service.IncaeSureService;
import top.infopub.job.bpa.service.OtherCotService;
import top.infopub.job.bpa.service.OtherSureService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.bpa.service.ProContractService;
import top.infopub.job.bpast.service.BpastCostCollectService;
import top.infopub.job.bpast.service.CostCollectService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.bpast.CostCollect;
import top.infopub.model.hr.OrgInfo;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;

@Service
public class BpastCostCollectServiceImpl implements BpastCostCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private CostCollectService costCollectService;

    @Autowired
    private DeviceSureService deviceSureService;

    @Autowired
    private OtherCotService otherCotService;

    @Autowired
    private OtherSureService otherSureService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bpastCostCollectJob() {

        //查询所有项目合同名称
        Map<String, Object> proContractWhere = Maps.newHashMap();
        proContractWhere.put("processStatus", "9");
        List<ProContract> proContracts = proContractService.selectByMapWhere(proContractWhere);
        for (ProContract proContract : proContracts) {
            CostCollect costCollect = new CostCollect();
            //往表里存放项目信息
            costCollect.setProCode(proContract.getProCode());
            costCollect.setProName(proContract.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(proContract.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                costCollect.setOrgProName(orgInfos.get(0).getCompName());
                costCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    costCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    costCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            costCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            costCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            costCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //建安费(元)
            Map<String, Object> innerSureWhere = Maps.newHashMap();
            innerSureWhere.put("sureMonth", costCollect.getStsMonth());
            innerSureWhere.put("proCode", costCollect.getProCode());
            BigDecimal proQaOfInnerSure = incaeSureService.selectProQaOfInnerSure(innerSureWhere);
            costCollect.setCaeAmount(proQaOfInnerSure);
            //代付费(元)
            Map<String, Object> otherSureWhere1 = Maps.newHashMap();
            otherSureWhere1.put("sureMonth", costCollect.getStsMonth());
            otherSureWhere1.put("proCode", costCollect.getProCode());
            BigDecimal replaceAmount = otherSureService.selectMonthSureAmount32(otherSureWhere1);
            costCollect.setReplaceAmount(replaceAmount);
            //前期费(元)
            Map<String, Object> otherSureWhere2 = Maps.newHashMap();
            otherSureWhere2.put("sureMonth", costCollect.getStsMonth());
            otherSureWhere2.put("proCode", costCollect.getProCode());
            BigDecimal preAmount = otherSureService.selectMonthSureAmount33(otherSureWhere2);
            costCollect.setPreAmount(preAmount);
            //专业设备采购款(元)
            Map<String, Object> deviceSureWhere = Maps.newHashMap();
            deviceSureWhere.put("sureMonth", costCollect.getStsMonth());
            deviceSureWhere.put("proCode", costCollect.getProCode());
            BigDecimal deviceAmount = deviceSureService.selectMonthSureAmount(deviceSureWhere);
            costCollect.setDeviceAmount(deviceAmount);
            //合计(元)
            BigDecimal sumTotalAmount = costCollect.getDeviceAmount().add(
                costCollect.getPreAmount()).add(costCollect.getReplaceAmount()).add(
                costCollect.getCaeAmount());
            costCollect.setSumTotalAmount(sumTotalAmount);
            //数据状态(默认为有效)
            costCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            costCollect.setAddBy("system");
            costCollect.setAddTime(new Date());
            costCollect.setUptBy("system");
            costCollect.setUptTime(new Date());
            costCollectService.insert(costCollect);
        }
    }
}